package model.user;

import model.beans.OrderBean;
import core.Database;
import model.beans.RouterBean;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.LinkedList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import org.apache.log4j.Logger;

/**
 *
 * @author Danon
 */
@ManagedBean
@RequestScoped
public class UserProfileBean implements Serializable {

    private int id;
    private String fullName;
    private String login;
    private String password;
    
    private static final Logger logger = Logger.getLogger(UserProfileBean.class);
    
    /** Creates a new instance of UserProfileBean */
    public UserProfileBean() {
        
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getFullName() {
        return fullName;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
    
    /**
     * Returns a list of orders ordered by ID in descending order (recent orders first)
     * @return ArrayList of orders
     */
    public List<OrderBean> orders() {
        List<OrderBean> l = new LinkedList<>();
        try {
            Connection c = Database.getInstance().getConnection();
            try (PreparedStatement ps = c.prepareStatement("select order_id, customer_id, to_char(order_date, 'DD.MM.YYYY HH24:MI:SS') as order_date, "
                    + "to_char(completion_date, 'DD.MM.YYYY HH24:MI:SS') as completion_date, src_router, dst_router, bandwidth, status, order_comment "
                         + "from orders where customer_id = ? order by order_id desc")) {
                ps.setInt(1, getId());
                ResultSet rs = ps.executeQuery();
                while(rs.next()) {
                    OrderBean order = new OrderBean();
                    order.setId(rs.getInt("order_id"));
                    order.setUserId(rs.getInt("customer_id"));
                    order.setBandwidth(rs.getFloat("bandwidth"));
                    order.setOrderDate(rs.getString("order_date"));
                    order.setComment(rs.getString("order_comment"));
                    order.setCompletionDate(rs.getString("completion_date"));
                    order.setSourceRouterName(RouterBean.loadById(rs.getInt("src_router")).getName());
                    order.setDestinationRouterName(RouterBean.loadById(rs.getInt("dst_router")).getName());
                    order.setStatus(rs.getString("status"));
                    l.add(order);
                }
                rs.close();
            }
        } catch(Exception ex) {
            logger.debug("Database problem: "+ex.getMessage());
        }
        return l;
    } 
    
    public String saveToDB() {
        try {
            Connection c = Database.getInstance().getConnection();
            try (PreparedStatement ps = c.prepareStatement("update users set password = ?, full_name = ? where login = ?")) {
                ps.setString(1, getPassword());
                ps.setString(2, getFullName());
                ps.setString(3, getLogin());
                ps.executeUpdate();
            }
            c.commit();
        } catch (Exception ex) {
            logger.debug("Database problem: "+ex.getMessage());
            return "welcome?faces-redirect=true";
        }
        return "welcome?faces-redirect=true";
    }
    
}
